/**
 * 
 */
package fr.esiag.projet17.action;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.security.PrivilegedAction;
import java.util.Properties;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import fr.esiag.projet17.client.transfert.CredentialsElement;
import fr.esiag.projet17.client.transfert.KeyElement;
import fr.esiag.projet17.client.transfert.ObjectFactory;

/**
 * @author teddy
 *
 */
public class UtilisateurAction implements PrivilegedAction<byte[]>{

	final Properties props;
	private String login;
	private String password;
	
	public UtilisateurAction(Properties props) {
		this.props = props;
	}
	
	@Override
	public byte[] run() {
		try {
			JAXBContext context = JAXBContext.newInstance(CredentialsElement.class);
			Marshaller marshaller = context.createMarshaller();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			CredentialsElement credentialsElement = new ObjectFactory().createCredentialsElement();
			credentialsElement.setLogin(login);
			credentialsElement.setPassword(password);
			marshaller.marshal(credentialsElement, baos);
			
			
			Integer port = Integer.parseInt(props.getProperty("rmi.port"));
			String url = props.getProperty("rmi.protocol")+ props.getProperty("rmi.host") +":"+ props.getProperty("rmi.port");
				
			Registry registry = LocateRegistry.getRegistry(props.getProperty("rmi.host"), port);
			Remote userDAO = (Remote) registry.lookup(url+props.getProperty("rmi.service.utilisateur"));
			
			for (int i = 0; i < userDAO.getClass().getInterfaces().length; i++) {
				System.out.println(userDAO.getClass().getInterfaces()[i].getName());
			}

			Method[] declaredMethod = userDAO.getClass().getDeclaredMethods();
			
			for (Method method : declaredMethod) {
				System.out.println(method.getName());
				if (method.getName().equals(props.getProperty("dao.utilisateur.readByCredentials"))){
					for (Class<?> c : method.getParameterTypes()){
							System.out.println(c.getName());						
					}
					Object partypes[] = new Object[1];
					partypes[0] = baos.toByteArray();
					Class<?>[] args = new Class[partypes.length];
					for (int i = 0; i < partypes.length; i++) {
						args[i] = partypes[i].getClass();
					}
					//Method m = userDAO.getClass().getDeclaredMethod(props.getProperty("dao.read"), args);
					System.out.println("invoke :" + method.getName());
					Object user = method.invoke(userDAO, partypes);
					if (user instanceof byte[]){
						return (byte[])user;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "OK".getBytes();
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
